﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.Xml;
using System.IO;
using System.Drawing;

namespace Food
{
	class MiscHelper
	{
		

		public static Point ParsePoint(string input)
		{
			string [] xy = input.Split(' ');

			if (xy.Length != 2)
					throw new ArgumentException("Please specify two integers separated with space.");

			return new Point(Convert.ToInt32(xy[0]), Convert.ToInt32(xy[1]));
		}

		public static Size ParseSize(string input)
		{
			string[] xy = input.Split(' ');

			if (xy.Length != 2)
				throw new ArgumentException("Please specify two integers separated with space.");

			return new Size(Convert.ToInt32(xy[0]), Convert.ToInt32(xy[1]));
		}

		public static string GetParameterFromXml(XmlNode xmlNode, string attributeName, string defaultValue)
		{
			if (xmlNode.Attributes[attributeName] == null)
				return defaultValue;

			return xmlNode.Attributes[attributeName].Value.ToString();
		}

		public static int GetParameterFromXml(XmlNode xmlNode, string attributeName, int defaultValue)
		{
			if (xmlNode.Attributes[attributeName] == null)
				return defaultValue;

			return Convert.ToInt32(xmlNode.Attributes[attributeName].Value);
		}

		public static bool GetParameterFromXml(XmlNode xmlNode, string attributeName, bool defaultValue)
		{
			if (xmlNode.Attributes[attributeName] == null)
				return defaultValue;

			return Convert.ToBoolean(xmlNode.Attributes[attributeName].Value);
		}

		public static string GetParameterFromReg(RegistryKey key, string name, string defaultValue)
		{
			if (key == null) return defaultValue;

			if (key.GetValue(name) != null)
				return key.GetValue(name).ToString();
			else
				return defaultValue;
		}

		public static int GetParameterFromReg(RegistryKey key, string name, int defaultValue)
		{
			if (key == null) return defaultValue;

			if (key.GetValue(name) != null)
				return Convert.ToInt32(key.GetValue(name));
			else
				return defaultValue;
		}

		public static bool GetParameterFromReg(RegistryKey key, string name, bool defaultValue)
		{
			if (key == null) return defaultValue;
			
			if (key.GetValue(name) != null)
				return Convert.ToBoolean(key.GetValue(name));
			else
				return defaultValue;
		}

		public static void SetParameterToReg(RegistryKey key, string name, string value)
		{
			key.SetValue(name, value);
		}

		public static void SetParameterToReg(RegistryKey key, string name, int value)
		{
			key.SetValue(name, value);
		}

		public static void SetParameterToReg(RegistryKey key, string name, bool value)
		{
			key.SetValue(name, value);
		}

		public static string GetRelativeFileName(string baseFileName, string fileName)
		{
			string basePath = Path.GetDirectoryName(baseFileName);
			
			if (!fileName.ToUpper().StartsWith(basePath.ToUpper())) return fileName;
			
			return fileName.Remove(0, basePath.Length + 1);
		}

	}
}
